<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/PearAdmin/component/layui/css/layui.css}" />
    <link rel="stylesheet" th:href="@{/PearAdmin/admin/css/pearCommon.css}"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <input type="hidden" id="userId" name="id" th:value="${MyUser.id}">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>用户名
                    </label>
                    <div class="layui-input-block">
                        <input type="text" id="userName" th:value="${MyUser.userName}" name="userName" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>昵称</label>
                    <div class="layui-input-block">
                        <input type="text" id="nickName" th:value="${MyUser.nickName}" name="nickName" required="" lay-verify="nikename" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>手机
                    </label>
                    <div class="layui-input-block">
                        <input type="text" id="phone" th:value="${MyUser.phone}" name="phone" required="" lay-verify="phone" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>邮箱
                    </label>
                    <div class="layui-input-block">
                        <input type="text"  name="email" th:value="${MyUser.email}"lay-verify="email" autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="激活"  th:checked="${MyUser.status == 1}? 'true':'false'">
                        <input type="radio" name="status" value="0" title="禁用" th:checked="${MyUser.status == 0}? 'true':'false'">
                    </div>
                </div>
                <input type="text" id="roleId"  name="roleId" style="display:none;"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block show-role-container">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/PearAdmin/component/layui/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form','jquery'],function(){
        let form = layui.form;
        $ = layui.jquery;
        getAllRole(function(){
            layui.form.render('radio')
        });
        form.on('submit(user-save)', function(data){
            $.ajax({
                url:'/api/user',
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType:'application/json',
                type:'put',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.location.reload();//刷新页面
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
        //自定义验证规则
        form.verify({
            nikename: function(value){
                if(value.length < 3){
                    return '昵称至少得3个字符啊';
                }
            }
            ,phone: [/(.+){11,11}$/, '电话号码必须11位']
            ,email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
        });
    })

    function getAllRole(callback){
        $.ajax({
            url:"/api/role/all",
            type:"GET",
            dataType:'json',
            success:function(result){
                var html = "";
                if(result.data && result.data.length > 0){
                    for(i in result.data){
                        html += '<input type="radio"  name="roleId" value="'+result.data[i].id+'" title="'+result.data[i].name+'" >' +
                            '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><div>'+result.data[i].name+'</div></div>';
                    }
                    $(".show-role-container").html(html);
                    getCurrentUserRole(callback);
                }
            }
        });
    }
    function getCurrentUserRole(callback){
        var userId = $("#userId").val();
        $.ajax({
            url:"/api/roleuser",
            type:"POST",
            data:{
                "userId" : userId
            },
            dataType:'json',
            success:function(result){
                if(result.code == 200){
                    if(result.data){
                        var str1 = JSON.stringify(result.data);
                        if (str1 == "[]"){
                            str1 = "[{'roleId': null}]"
                        }
                        var str = str1.substring(1,str1.length-1)
                        var obj = eval('('+str+')');
                        $("[name=roleId]").each(function () {
                            if($(this).val() == obj.roleId){
                                $(this).attr("checked","checked");
                                return false;
                            }
                        })
                    }
                    if (callback){
                        callback();
                    }
                }
            }
        });
    }
</script>
<script>
</script>
</body>
</html>
